package com.cdy.jxc_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdy.jxc_project.domain.TCustomer;
import com.cdy.jxc_project.domain.TPurchaseList;
import com.cdy.jxc_project.domain.TPurchaseListGoods;
import com.cdy.jxc_project.domain.TSaleList;
import com.cdy.jxc_project.domain.TSaleListGoods;
import com.cdy.jxc_project.domain.TSupplier;
import com.cdy.jxc_project.domain.vo.InventoryQuantityVo;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.mapper.TSaleListGoodsMapper;
import com.cdy.jxc_project.mapper.TSaleListMapper;
import com.cdy.jxc_project.service.TCustomerService;
import com.cdy.jxc_project.utils.AddSysLog;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/5/5 19:00
 * */
@RestController
@RequestMapping("/tSale")
public class TSaleController {
    /*
     * 销售管理与进货管理功能差不多一样
     * service接口与实现层直接省略
     * */

    @Resource
    private TSaleListMapper tSaleListMapper;

    @Resource
    private TSaleListGoodsMapper tSaleListGoodsMapper;

    @Resource
    private TCustomerService tCustomerService;

    @PostMapping("/queryAll")
    public R queryAll(){
        List<TSaleList> tSaleLists = tSaleListMapper.selectList(null);
        return R.ok().message("查询成功").data("tSaleLists",tSaleLists);
    }
    //生成入库单号
    @PostMapping("/makeSheet")
    public R makeSheet(){
        String rkSheet= "XS" +System.currentTimeMillis()+new Random().nextInt(9999);
        List<TCustomer> list = tCustomerService.list();
        return R.ok().data("sheetCode",rkSheet).data("customer",list);
    }

    @PostMapping("/querytSaleList")
    public R querytSaleList(@RequestBody TSaleList tSaleList){
        List<TSaleList> tSaleLists = tSaleListMapper.queryTSaleList(tSaleList);
        return R.ok().message("查询成功").data("tSaleLists",tSaleLists);
    }

    @PostMapping("/querytSaleListGoodsBytSaleListId/{id}")
    public R querytSaleListGoodsBytSaleListId(@PathVariable Integer id){
        QueryWrapper<TSaleListGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sale_list_id",id);
        List<TSaleListGoods> tSaleListGoods = tSaleListGoodsMapper.selectList(queryWrapper);
        return R.ok().message("查询成功").data("tSaleListGoods",tSaleListGoods);
    }

    /*
    * 销售出库操作
    * */

    @PostMapping("/insertTSaleList")
    public R insertTSaleList(@RequestBody TSaleList tSaleList){
        tSaleListMapper.insert(tSaleList);
        Integer id = tSaleList.getId();
        return R.ok().data("saleListId",id);
    }
    @PostMapping("/insertTSaleListGoods")
    public R insertTSaleListGoods(@RequestBody TSaleListGoods tSaleListGoods){
        tSaleListGoodsMapper.insert(tSaleListGoods);
        return R.ok().message("新增成功");
    }

}
